package com.aa.base

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Process
import android.view.Gravity
import android.webkit.WebView
import android.widget.Toast
import com.aa.base.sp.AppSp
import java.util.*

/**
 * @author Afra55
 * @date 4/15/21
 * A smile is the best business card.
 */
@SuppressLint("StaticFieldLeak")
object PlayerCheck {

    @JvmStatic
    fun init(
        applicationContext: Application,
        debug: Boolean,
        isPlayer: Boolean,
        forAdAccount: Boolean,
        showAppAd: Boolean
    ) {
        context = applicationContext
        needShowAppAd = showAppAd
        isDebugMode = debug
        isForAdAccount = forAdAccount
        this.isPlayer = isPlayer
        packageName = applicationContext.packageName

        val sharedPreferences = applicationContext.getSharedPreferences(
            "first_open",
            Context.MODE_PRIVATE
        )
        val firstOpen = sharedPreferences.getLong("first_open_time", 0)
        if (firstOpen == 0L) {

            val currentTimeMillis = System.currentTimeMillis()
            sharedPreferences.edit().putLong("first_open_time", currentTimeMillis).apply()
            firstOpenTime = currentTimeMillis
        } else {
            firstOpenTime = firstOpen
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val processName: String = getCurrentProcess(applicationContext)
            if (!applicationContext.packageName.equals(processName)) {
                WebView.setDataDirectorySuffix(processName)
            }
        }

    }



    @JvmStatic
    var context:Context? = null
    @JvmStatic
    var packageName:String = "com.aa.base"
    @JvmStatic
    var isDebugMode = false
    @JvmStatic
    var isPlayer = false
    @JvmStatic
    var needShowAppAd = false
    @JvmStatic
    var isForAdAccount = false

    @JvmStatic
    var need_show_time = "2021-06-15 01:00:00"

    @JvmStatic
    var af_status:String
        get() = AppSp.getStringValue("af_status", "")?:""
        set(value) =  AppSp.setValue("af_status", value)

    @JvmStatic
    fun isNotOrganic():Boolean{
        return af_status.isNotEmpty() && ( af_status.toLowerCase(Locale.US) != "organic")
    }

    @JvmStatic
    var firstOpenTime = 0L

    @JvmStatic
    fun getFirstOpenTimeNotNull():Long {
        return if (firstOpenTime == 0L) {
            context?.getSharedPreferences("first_open", Context.MODE_PRIVATE)
                ?.getLong("first_open_time", System.currentTimeMillis())?:System.currentTimeMillis()
        } else {
            firstOpenTime
        }
    }

    @JvmStatic
    fun isCanStartService(): Boolean {

        if (System.currentTimeMillis() >= BaseBatteryHelpers.string2Milliseconds(
                need_show_time
            )
        ){
            return true
        }
        return false
    }

    /**
     * 是否无限制
     */
    @JvmStatic
    fun isNoLimit():Boolean{
        return isNotOrganic() || isPlayer || isDebugMode
    }

    /**
     * 是否展示应用内广告
     */
    @JvmStatic
    fun isShowAppAd() = needShowAppAd || isNoLimit()

    fun getCurrentProcess(context: Context): String {
        var currentProcessName = ""
        val manager =
            context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == Process.myPid()) {
                currentProcessName = processInfo.processName
                break
            }
        }
        return currentProcessName
    }
}

fun String.toast(gravity:Int = Gravity.BOTTOM){
    PlayerCheck.context?.let {
        val makeText = Toast.makeText(it, this, Toast.LENGTH_SHORT)
        makeText.setGravity(gravity, 0, 0)
        makeText.show()
    }
}